package cn.edu.ccut.refactor;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.DrillDownComposite;

/**
 * 
 * @author Bai Yucheng 2008-1-21
 * 
 */
public class MoveInputWizardPage extends UserInputWizardPage {
	public static boolean flag = false;
	private TreeViewer fTreeViewer;
	private String fMessage;
	private IProject fCurrentProject;

	public MoveInputWizardPage(String s, IProject saturnProject) {
		super("startPage");  //$NON-NLS-1$
		fMessage = s;
		fCurrentProject = saturnProject;
	}

	public void createControl(Composite composite) {
		Composite composite1 = new Composite(composite, 0);
		setControl(composite1);
		initializeDialogUnits(composite1);
		composite1.setLayout(new GridLayout());
		createTreeViewer(composite1);
		new GridLayout();
		final Button button = new Button(composite1, 32);
		button.setSelection(true);
		RenameRefactoring renamerefactoring = (RenameRefactoring) getRefactoring();
		RefactoringProcessor refactoringprocessor = renamerefactoring
				.getProcessor();
		((AbstractRenameProcessor) refactoringprocessor)
				.setUpdateReference(button.getSelection());
		button.setText(Messages.getString("MoveInputWizardPage.1"));  //$NON-NLS-1$
		button.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent selectionevent) {
				handleReference(button.getSelection());
			}

		});
	}

	private void handleReference(boolean flag) {
		RenameRefactoring renamerefactoring = (RenameRefactoring) getRefactoring();
		RefactoringProcessor refactoringprocessor = renamerefactoring
				.getProcessor();
		((AbstractRenameProcessor) refactoringprocessor)
				.setUpdateReference(flag);
	}

	private void createTreeViewer(Composite composite) {
		Composite composite1 = new Composite(composite, 0);
		GridLayout gridlayout = new GridLayout();
		gridlayout.verticalSpacing = 9;
		composite1.setLayout(gridlayout);
		composite1.setLayoutData(new GridData(1808));
		Label label = new Label(composite1, 64);
		label.setText(fMessage);
		label.setFont(composite.getFont());
		DrillDownComposite drilldowncomposite = new DrillDownComposite(
				composite1, 2048);
		GridData griddata = new GridData(1808);
		griddata.widthHint = 320;
		griddata.heightHint = 300;
		drilldowncomposite.setLayoutData(griddata);
		fTreeViewer = new TreeViewer(drilldowncomposite, 772);
		drilldowncomposite.setChildTree(fTreeViewer);
		fTreeViewer.setContentProvider(new WorkbenchContentProvider());
		fTreeViewer.setLabelProvider(new WorkbenchLabelProvider());
		fTreeViewer.addFilter(new FileFilter(MoveSaturnAction.folderName));
		if (fCurrentProject == null)
			fTreeViewer.setInput(ResourcesPlugin.getWorkspace());
		else
			fTreeViewer.setInput(fCurrentProject);
		fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

					public void selectionChanged(
							SelectionChangedEvent selectionchangedevent) {
						IStructuredSelection istructuredselection = (IStructuredSelection) selectionchangedevent
								.getSelection();
						viewerSelectionChanged(istructuredselection
								.getFirstElement());
					}
				});
		fTreeViewer.addDoubleClickListener(new IDoubleClickListener() {

			public void doubleClick(DoubleClickEvent doubleclickevent) {
				ISelection iselection = doubleclickevent
						.getSelection();
				if (iselection instanceof IStructuredSelection) {
					Object obj = ((IStructuredSelection) iselection)
							.getFirstElement();
					if (fTreeViewer.getExpandedState(obj))
						fTreeViewer.collapseToLevel(obj, 1);
					else
						fTreeViewer.expandToLevel(obj, 1);
				}
			}
		});
		viewerSelectionChanged(null);
	}

	protected void viewerSelectionChanged(Object obj) {
		RefactoringProcessor refactoringprocessor = ((RenameRefactoring) getRefactoring())
				.getProcessor();
		Object aobj[] = ((AbstractRenameProcessor) refactoringprocessor)
				.getElements();
		IFile tianFile = (IFile) aobj[0];
		IContainer tianFolder = tianFile.getParent();
		if (obj != null && (obj instanceof IContainer)) {
			IContainer saturnCon = (IContainer) obj;
			if (tianFolder.getFullPath().equals(saturnCon.getFullPath())
					|| !saturnCon.getName().matches(".*(action|logic|component|validate|data|" +
							"workflow|web|page|Action|Logic|Component|Validate|Data|Workflow|Web|Page)")) {  //$NON-NLS-1$
				setPageComplete(false);
				return;
			} else {
				setPageComplete(true);
				((AbstractRenameProcessor) refactoringprocessor)
						.setDestFolder((IFolder) saturnCon);
				((AbstractRenameProcessor) refactoringprocessor)
						.setMoveFile(true);
				return;
			}
		}
		setPageComplete(false);
	}

//	public void setVisible(boolean visible) {
//		flag = !visible;
//		super.setVisible(visible);
//	}

}